﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using YEngineLib;
using YEngineLib.Animation;

using Spyfall.Managers;

namespace Spyfall.Objects
{
    public class Player : Object
    {
        public Texture2D idleTexture;
        List<Animation> playerAnimationList;
        Animation idleAnimation;
        public Texture2D rightTexture;
        Animation rightAnimation;
        Animation currentAnimation;
        public Vector2 origin;
        public Vector2 position;
        public Vector2 velocity;

        public float snapTimer;
        public bool snapUp;
        public bool snapDown;
        public float upLimit;
        public float downLimit;

        public bool dashRight;
        public bool dashLeft;

        public float dashTraveled;

        public float tempX;


        public enum Rows { TOP = 260, BOTTOM = 470, }

        public Player()
        {
            ObjectID = Object_Type.PLAYER;

            snapUp = false;
            snapDown = false;

            dashRight = false;
            dashLeft = false;

            upLimit = 260;
            downLimit = 470;

            position.X = 500;
            position.Y = 260;

            snapTimer = 0.0f;

            dashTraveled = 0.0f;

            // Animation Stuff Start ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            idleTexture = YEngine.Game.Content.Load<Texture2D>("Graphics/dive");
            rightTexture = YEngine.Game.Content.Load<Texture2D>("Graphics/tiltright");
            playerAnimationList = new List<Animation>();
            idleAnimation = new Animation();
            idleAnimation.Initialize(idleTexture, position, 150, 196, 6, 0.1f, Color.White, 1f, true);
            rightAnimation = new Animation();
            rightAnimation.Initialize(rightTexture, position, 150, 196, 4, 0.1f, Color.White, 1f, true);
            playerAnimationList.Add(idleAnimation);
            playerAnimationList.Add(rightAnimation);
            currentAnimation = idleAnimation;

            ColRect = new Rectangle(int.Parse(position.X.ToString()), int.Parse(position.Y.ToString()), idleTexture.Bounds.Width / 2, idleTexture.Height / 2);
            // Animation Stuff End   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            //speedUpNum = levelmanager speedUpNum 

        }

        public override void Update(float deltaTime)
        {
            // Animation Stuff Start ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            for (int i = playerAnimationList.Count - 1; i >= 0; i--)
            {
                playerAnimationList[i].Update(deltaTime);
                if (playerAnimationList[i].Active == false)
                {
                    playerAnimationList.RemoveAt(i);
                }
            }
            // Animation Stuff End   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            ColRect = new Rectangle((int)position.X - 50, (int)position.Y - 50, 100, 100);

            if (velocity.X > 0.0f)
            {
                currentAnimation = rightAnimation;
                //ColRect = new Rectangle(int.Parse(position.X.ToString()), int.Parse(position.Y.ToString()), rightTexture.Bounds.Width, rightTexture.Height);
                //playerAnimationList.RemoveAt(0);
                //playerAnimationList.Add(rightAnimation);
                //playerAnimationList.Active = false;
                //playerAnimationList[1].Active = true;
            }
            else
            {
                currentAnimation = idleAnimation;
                //ColRect = new Rectangle(int.Parse(position.X.ToString()), int.Parse(position.Y.ToString()), idleTexture.Bounds.Width, idleTexture.Height);
                //     playerAnimationList.RemoveAt(0);
                //     playerAnimationList.Add(idleAnimation);
            }

            if (dashRight == true)
            {
                if (position.X > 1200)
                {
                    dashRight = false;
                }
                else
                {

                    position.X += deltaTime * GameManager.GetInstance.DashSpd;
                    dashTraveled += deltaTime * GameManager.GetInstance.DashSpd;

                    if (dashTraveled >= GameManager.GetInstance.DashLength)
                    {
                        dashRight = false;
                        dashTraveled = 0.0f;
                    }
                }
            }

            if (dashLeft == true)
            {
                if (position.X < 50)
                {
                    dashLeft = false;
                }
                else
                {

                    position.X -= deltaTime * GameManager.GetInstance.DashSpd;
                    dashTraveled += deltaTime * GameManager.GetInstance.DashSpd;

                    if (dashTraveled >= GameManager.GetInstance.DashLength)
                    {
                        dashLeft = false;
                        dashTraveled = 0.0f;
                    }
                }
            }



            if (snapDown == false && snapUp == false && dashRight == false && dashLeft == false)
            {
                tempX = position.X;
                tempX += (velocity.X * deltaTime) * 400;

                if (tempX >= 50 && tempX <= 1220)
                {
                    position.X = tempX;
                }
            }

            //position.Y += (velocity.Y*deltaTime)*400;
            //&& position.X >= 30 && position.X <= 1230

            if (snapUp == true)
            {
                if (position.Y == upLimit)
                {
                    snapUp = false;
                }
                else
                {

                    position.Y -= deltaTime * 200;

                    if (position.Y <= upLimit)
                    {
                        snapUp = false;
                    }
                }
            }

            if (snapDown == true)
            {
                if (position.Y == downLimit)
                {
                    snapDown = false;
                }
                else
                {

                    position.Y += deltaTime * 300;

                    if (position.Y >= downLimit)
                    {
                        snapDown = false;
                    }
                }
            }


            //idleAnimation.Position = position;
            //playerAnimationList.ElementAt(0).Position = position;
            //currentAnimation.Position = position;
            for (int i = playerAnimationList.Count - 1; i >= 0; i--)
            {
                playerAnimationList[i].Position = position;
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {

            // Animation Stuff Start ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            // for (int i = 0; i < playerAnimationList.Count; i++)
            // {
            //     playerAnimationList[i].Draw(spriteBatch);
            // }
            currentAnimation.Draw(spriteBatch);
            // Animation Stuff End   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            //YEngine.Graphics.Line.DrawRect(spriteBatch, new Rectangle(int.Parse(ColRect.X.ToString()), int.Parse(ColRect.Y.ToString()), int.Parse(ColRect.Width.ToString()), int.Parse(ColRect.Height.ToString())), 5.0f, Color.Black);
        }

        public override bool Collide(Object tar)
        {
            bool res = ColRect.Intersects(tar.ColRect);
            return res;
        }

        // internal static void Draw(SpriteBatch spriteBatch)
        // {
        //     throw new NotImplementedException();
        //}


    }
}
